package chat.exit;

import pojo.User;
import utils.ServerConfig;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;

//退出服务器
public class Exit {
    private User user;
    public Exit(User user){
        this.user=user;
    }

    public void run() {
        try {
            //使用udp向服务器发送用户ip
            //服务器将其对应的ip退出
            DatagramSocket socket = new DatagramSocket();
            DatagramPacket packet = new DatagramPacket(user.getName().getBytes(StandardCharsets.UTF_8),0,user.getName().getBytes(StandardCharsets.UTF_8).length,
                    InetAddress.getByName(ServerConfig.serverConfigString("InetAddress")),ServerConfig.serverConfig("ServerExitPort"));
            socket.send(packet);
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
